package com.ls.pattern.chain.auth.optimize;

/**
 * @author 挥之以墨
 */
public abstract class Handler<T, R> {

    protected Handler<T, R> next;

    public void next(Handler<T, R> next) {
        this.next = next;
    }

    public static class Builder<T, R> {
        private Handler<T, R> head;
        private Handler<T, R> tail;

        public Builder<T, R> addHandler(Handler<T, R> handler) {
            if (null == this.head) {
                this.head = this.tail = handler;
            } else {
                this.tail.next(handler);
                this.tail = handler;
            }
            return this;
        }

        public Handler<T, R> build() {
            return this.head;
        }

    }

    public abstract R doHandler(T t);
}
